package com.bocai.grainshop.seckill.controller;

import com.bocai.common.utils.R;
import com.bocai.grainshop.seckill.service.SeckillService;
import com.bocai.grainshop.seckill.vo.SkukillRedisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    
    @Autowired
    private SeckillService seckillService;
    
    /**
     * @Author: SunJinbo
     * @Description  获取当前可以秒杀的商品
     * @Param []
     * @return com.bocai.common.utils.R      
     * @Data Created in 2022/4/28 18:30
     */
    @ResponseBody
    @GetMapping("/seckillNow")
    public R getSeckillNowShop(){
        List<SkukillRedisVo> vo = seckillService.getSeckillNowShop();
        return R.ok().put("data",vo);
    }

    /**
     * @Author: SunJinbo
     * @Description  获取某个商品的秒杀信息
     * @Param [skuId]
     * @return com.bocai.common.utils.R
     * @Data Created in 2022/4/28 20:53
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSeckillMsgBySkuId(@PathVariable("skuId") Long skuId){
        SkukillRedisVo to = seckillService.getSeckillMsgByskuId(skuId);
        return R.ok().put("to",to);
    }

    /**
     * @Author: SunJinbo
     * @Description  秒杀的方法
     * @Param [killId, key, num]
     * @return com.bocai.common.utils.R
     * @Data Created in 2022/4/30 17:23
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model){
        //秒杀成功返回订单号即可
        String orderSn = seckillService.seckill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
